﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ConsoleApp.Util;
using Just.Library.Extensions;
using Just.Library.Util;

namespace ConsoleApp.Infrastructure.TaskFramework
{
    public abstract class TaskBase : ITask
    {
        protected bool IsDebug
        {
            get
            {
                return AppSettingsConstVars.Debug;
            }
        }
        protected virtual int TaskEndCode
        {
            get { return DateTime.Today.AddDays(-2).ToDateCode(); }
        }
        protected virtual int TaskEarliestCode
        {
            get { return 20200101; }
        }

        /// <summary>
        /// 统计近两年数据(月份)
        /// </summary>
        protected int TaskStartMonthCode => new DateTime(DateTime.Now.Year - 2, 1, 1).ToDateCode() / 100;

        /// <summary>
        /// 近半年
        /// </summary>
        protected virtual int NearlyHalfYearCode
        {
            get { return DateTime.Now.AddMonths(-6).ToDateCode(); }
        }

        /// <summary>
        /// TaskClassName
        /// </summary>
        protected virtual string TaskClassName
        {
            get { return GetType().Name; }
        }

        /// <summary>
        /// 触发任务执行异常事件
        /// </summary>
        /// <param name="ex">异常信息</param>
        /// <param name="message">描述信息</param>
        protected virtual void OnTaskException(Exception ex, string message)
        {
            if (TaskException != null)
            {
                TaskException(this, ex, message);
            }
        }

        /// <summary>
        /// 触发任务执行过程信息事件
        /// </summary>
        /// <param name="message">描述信息</param>
        /// <param name="是否持久化">描述信息</param>
        protected virtual void OnTaskLog(string message, bool persistence = false)
        {
            if (TaskLog != null)
            {
                TaskLog(this, message, persistence);
            }
        }

        /// <summary>
        /// 将线程休息指定的时间
        /// </summary>
        /// <param name="timeSpan">休眠时间</param>
        protected void SleepThread(TimeSpan timeSpan)
        {
            double sleepMilliseconds = timeSpan.TotalMilliseconds;
            int sleepStep = 600000;
            while (sleepMilliseconds > sleepStep)
            {
                Thread.Sleep(sleepStep);
                sleepMilliseconds -= sleepStep;
                OnTaskLog(this + "剩余时间:" + sleepMilliseconds / 1000.0);
            }

            if (sleepMilliseconds > 0)
            {
                Thread.Sleep(Convert.ToInt32(sleepMilliseconds));
            }
        }

        #region 给飞书机器人发送消息
        public void SendMsgByFeiShu(List<string> webhooks, string text_msg)
        {
            if (!AppSettingsConstVars.Debug)
            {
                FeiShuMsgHelper.SendTextMsg(webhooks, text_msg);
            }
        }
        public void SendMsgByFeiShu(string webhook, string text_msg)
        {
            if (!AppSettingsConstVars.Debug)
            {
                FeiShuMsgHelper.SendTextMsg(webhook, text_msg);
            }
        }
        public void SendMsgByFeiShu(string text_msg, bool isError = true)
        {
            if (!AppSettingsConstVars.Debug)
            {
                FeiShuMsgHelper.SendTextMsg(isError ? FeiShuMsgHelper.Error_Webhooks : FeiShuMsgHelper.Webhooks, text_msg);
            }
        }
        #endregion

        /// <summary>
        /// 任务延后执行
        /// </summary>
        /// <param name="action"></param>
        /// <param name="delayHours">默认延后1小时</param>
        protected void DelayRunTask(Action action, int delayHours = 1)
        {
            if (DateTime.Now.Hour >= delayHours)
                action.Invoke();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="action">任务方法</param>
        /// <param name="timeFilter">时间过滤条件</param>
        protected void RunTask(Action action, Func<DateTime, bool> timeFilter)
        {
            if (timeFilter.Invoke(DateTime.Now))
            {
                action.Invoke();
            }
        }

        #region ITask Members

        public event TaskLogHandler TaskLog;

        public event TaskExceptionHandler TaskException;
        public virtual void Execute()
        {
            ExecuteAsync();
        }

        public virtual Task ExecuteAsync()
        {
            return Task.CompletedTask;
        }
        /// <summary>
        /// 判断当前任务是否正在执行
        /// </summary>
        public bool IsRunning { get; set; }
        /// <summary>
        /// 获取或设置任务优先级
        /// </summary>
        public ThreadPriority Priority { get; set; }
        /// <summary>
        /// 任务描述信息
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// TaskCount
        /// </summary>
        public int TaskCount { get; set; }
        #endregion
    }
}
